﻿/*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
*/
using System.Collections.Specialized;
using System.Linq;
using System;
using System.Management;
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using System.IO;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;

namespace SCCMHealthCheck
{
  class WMIUtil
  {
    const string gRootConnectionPath = @"\\{0}\root\cimv2";
    const string gSMSConnectionPath = @"\\{0}\root\sms";

    private ManagementScope _rootscope = null;
    private ManagementScope _smsscope = null;

    public ManagementScope RootScope
    {
      get { return _rootscope; }
    }

    public ManagementScope SMSScope
    {
      get { return _smsscope; }
    }

    public void CreateScope(string NameSpace, string ServerName, string UserName, string Password)
    {
      ConnectionOptions _connOpt = new ConnectionOptions();
      _connOpt.Username = UserName;
      _connOpt.Password = Password;

      switch (NameSpace.ToLower())
      {
        case "root":
          if (_rootscope == null)
            _rootscope = new ManagementScope(string.Format(gRootConnectionPath, ServerName), _connOpt);

          if (!_rootscope.IsConnected)
            _rootscope.Connect();
          break;

        case "sms":
          if (_smsscope == null)
            _smsscope = new ManagementScope(string.Format(gSMSConnectionPath, ServerName), _connOpt);

          if (!_smsscope.IsConnected)
            _smsscope.Connect();
          break;
      }
    }

    public ManagementObjectSearcher WMIQuery(string NameSpace, string _SQL)
    {
      ObjectQuery _query = new ObjectQuery(_SQL);

      switch (NameSpace.ToLower())
      {
        case "sms":
          return new ManagementObjectSearcher(_smsscope, _query);
        default:
          return new ManagementObjectSearcher(_rootscope, _query);
      }
    }
  }
}
